var app = getApp()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //应急ID
        emergencyId:'',
        //起始应急的ID
        startPlanId:'',
        //当前任务ID
        taskId:'',
        //应急数据
        emerData:{},
        //值班列表（和起始应急ID绑定）
        dutyList:[],
        //总结信息
        emerSummary:{},
        //审核意见
        examineHidden:false,
        examineOpinion:'',
        popupShow:false,
        //任务类型
        taskDefineKey:''
    },

    /**
     * 生命周期函数--监听页面加载
     * */
    onLoad: function (options) {
        this.setData({
            emergencyId:options.emergencyId,
            startPlanId:options.startPlanId,
            taskId:options.taskId,
            taskDefineKey:options.taskDefineKey
        });
        this.getSummary();
        this.getEmergencyData();
        this.getDutyTableData();
    },
    getEmergencyData:function () {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };

        wx.showLoading()
        this.setData({
            emerData:{}
        })
        wx.request({
            url: app.globalData.baseUrl + '/safe/planPublic/find?id=' + this.data.emergencyId,
            header: header,
            method: "POST",
            success(res) {
                that.setData({
                    emerData:res.data.data.list[0]
                })
                //关闭加载
                wx.hideLoading()
            },
            fail(){
                //关闭加载
                wx.hideLoading()
            }
        })
    },
    getDutyTableData:function () {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={planPublicId:this.data.startPlanId};

        wx.showLoading()
        this.setData({
            dutyList:[]
        })
        wx.request({
            url: app.globalData.baseUrl + '/safe/duty/find',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                let tempArr=res.data.data.list;
                tempArr.forEach(function (item) {
                    let dateObj=new Date();
                    dateObj.setTime(item.dutyDate);
                    item.dutyDateStr=util.formatTime(dateObj);
                })
                that.setData({
                    dutyList:tempArr
                })
                //关闭加载
                wx.hideLoading()
            },
            fail(){
                //关闭加载
                wx.hideLoading()
            }
        })
    },
    //获取总结信息
    getSummary:function () {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={companyId:app.globalData.user.companyId,planPublicId:this.data.emergencyId};

        wx.showLoading()
        this.setData({
            dutyList:[]
        })
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgSummary/find',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                that.setData({
                    emerSummary:res.data.data.list[0]
                })
                //关闭加载
                wx.hideLoading()
            },
            fail(){
                //关闭加载
                wx.hideLoading()
            }
        })
    },
    editOpinion:function (e) {
        this.setData({
            examineOpinion:e.detail.value
        })
    },
    //提交或者退回
    doTaskClick:function (e) {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={taskId:this.data.taskId,comment:this.data.examineOpinion,result:e.currentTarget.dataset.result};

        wx.showLoading()
        this.setData({
            dutyList:[]
        })
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgFlow/doTask',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                //关闭加载
                wx.hideLoading()
                wx.showToast({
                    title: '提交成功',
                    icon: 'none',
                })
                setTimeout(function () {
                    wx.redirectTo({
                        url: '/pages/emergency/emerExamine/emerExamine',
                    })},1000);

            },
            fail(){
                //关闭加载
                wx.hideLoading()
                wx.showToast({
                    title: '提交失败',
                    icon: 'none',
                })
            }
        })
    },
    turnBackClick:function () {
        let that=this;
        wx.showModal({
            title: '提示',
            content: '确定退回该总结？',
            success (res) {
                if (res.confirm) {
                    that.doTaskClick({currentTarget:{dataset:{result:0}}})
                } else if (res.cancel) {

                }
            }
        })
    },
    showDetailClick:function () {
        this.setData({
            examineHidden:true,
            popupShow:true
        })
    },
    closeDetailClick:function () {
        this.setData({
            examineHidden:false,
            popupShow:false
        })
    },
    onClickLeft:function () {
        wx.redirectTo({
            url: '/pages/emergency/emerTaskList/emerTaskList?taskDefineKey=' + this.data.taskDefineKey
        })
    }
})